package com.ruijie.acl.web;
import java.util.ArrayList;
import java.util.List;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.jsf.ListDataModel;

import com.ruijie.acl.AccessGroup;
import com.ruijie.acl.AclDevice;
import com.ruijie.acl.AclDeviceAccessGroup;
import com.ruijie.acl.service.AccessGroupService;
import com.ruijie.acl.service.DeviceAccessGroupService;
import com.ruijie.emp.device.Device;
import com.ruijie.foundation.bo.EntityObject;
import com.ruijie.foundation.exception.BusinessException;
import com.ruijie.foundation.service.Repository;

/**
 * <p>
 * Title:在ACL组详细页面中管理ACL设备的ACTION

 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2008
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time: 
 * </p>
 * <p>
 * </p>
 * <p>
 * Update Time:
 * </p>
 * <p>
 * Updater:
 * </p>
 * <p>
 * Update Comments:
 * </p>
 * @author ASAI
 */
@Name("deviceManageInAccessGroupAction")
@Scope(ScopeType.CONVERSATION)
public class DeviceManageInAccessGroupAction extends AclPlanBaseAction{
	@In("#{deviceAccessGroupService}")
	private DeviceAccessGroupService deviceAccessGroupService;
	
	@In("#{accessGroupService}")
	private AccessGroupService accessGroupService;
	@In("#{accessGroupAction}")
	private AccessGroupAction accessGroupAction;
	
	//与ACL组关联的设备列表
	private ListDataModel aclDeviceEntityModelList = null;
	
	//ACl组信息
	private AccessGroup accessGroup;
	private Long groupId = null;
	
	
	/**
	 * 在组详细中，取得ACL设备分页信息
	 * @return 设备列表
	 */
	@SuppressWarnings("unchecked")
	public ListDataModel getAclDeviceEntityModelList(AccessGroup accessGroup) {
		if (accessGroup != null) {
			this.accessGroup = accessGroup;
			//if (aclDeviceEntityModelList == null || accessGroup.getId() != this.groupId) {
			adjustPage();
			currentPage = deviceAccessGroupService.pageQueryByAccessGroup(
						accessGroup, page, pageSize);
			aclDeviceEntityModelList = new ListDataModel(
						(List<AclDeviceAccessGroup>) currentPage.getResult());
			//}
			this.groupId = accessGroup.getId();
		}
		return aclDeviceEntityModelList;
	}
	
	/**
	 * 删除设备组之间的关联
	 * @param entity
	 */
	public void delete(AclDeviceAccessGroup entity) {
		deviceAccessGroupService.remove(entity);
		AccessGroup accessGroupDevice = accessGroupService.getByNameWithAclDevice(entity.getAccessGroup().getName(), entity.getAclDevice());
		if (accessGroupDevice != null){
			accessGroupService.remove(accessGroupDevice);
		}
		updateModel();
	}
	
	@Override
	protected void updateModel() {
		this.aclDeviceEntityModelList = null;
		getAclDeviceEntityModelList(accessGroup);		
	}
	
	/**
	 * 根据ACL组取得ACL设备列表
	 * @param entity ACL组
	 * @return ACL设备列表
	 */
	public List<AclDevice> getAclDeviceByAccessGroup(AccessGroup entity){
		List<AclDeviceAccessGroup> list = this.deviceAccessGroupService.getByAccessGroup(entity);
		List<AclDevice> aclDeviceList = new ArrayList<AclDevice>();
		if (list != null && list.size() > 0){
			for(AclDeviceAccessGroup aclDeviceAccessGroup : list){
				aclDeviceList.add(aclDeviceAccessGroup.getAclDevice());
			}
		}
		return aclDeviceList;
	}
	
	/**
	 * ACL组详细界面的单设备部署
	 * @param id
	 * @param device
	 */
	public void reConfig(String id,Device device){		
		AccessGroup group = null;
		try {
			group = accessGroupService.get(Long.valueOf(id));
			accessGroupService.reConfig(group, device, "modify");
		} catch (BusinessException e) {
			throw e;
		}
		accessGroupAction.resetNewestModel();
		updateModel();
		addGlobalI18nMessage("acl_device_deploy_success");
	}
	
	/**
	 * ACL组详细界面的删除设备

	 * @param id
	 * @param device
	 */
	public void deleteDeviceConfig(String id,Device device,AclDeviceAccessGroup entity){		
		AccessGroup group = null;
		try {
			group = accessGroupService.get(Long.valueOf(id));
			accessGroupService.reConfig(group, device, "DeleteAccessGroup");
		} catch (BusinessException e) {
			throw e;
		}
		entity = deviceAccessGroupService.get(entity.getId());
		this.delete(entity);
		accessGroupService.removeAclInterface(group,device);
		accessGroupAction.resetNewestModel();
		updateModel();
		
		addGlobalI18nMessage(getDelSuccessMsg());
	}
	
	
	
	public ListDataModel getAclDeviceEntityModelList() {
		return aclDeviceEntityModelList;
	}

	public void setAclDeviceEntityModelList(ListDataModel aclDeviceEntityModelList) {
		this.aclDeviceEntityModelList = aclDeviceEntityModelList;
	}

	@Override
	@SuppressWarnings("unchecked")
	protected Repository getRepository() {
		return deviceAccessGroupService;
	}

	@Override
	protected String getAddPage() {
		return null;
	}

	@Override
	public EntityObject getCriteria() {
		return null;
	}

	@Override
	protected String getDetailPage() {
		return null;
	}

	@Override
	protected String getListPage() {
		return null;
	}

	@Override
	public EntityObject getModel() {
		return null;
	}

	@Override
	protected String getUpdatePage() {
		return null;
	}

	@Override
	public void resetModel() {
	}

	@Override
	public void setCriteria(EntityObject criteria) {
	}

	@Override
	public void setModel(EntityObject model) {
	}

	public AccessGroup getAccessGroup() {
		return accessGroup;
	}

	public void setAccessGroup(AccessGroup accessGroup) {
		this.accessGroup = accessGroup;
	}

	public Long getGroupId() {
		return groupId;
	}

	public void setGroupId(Long groupId) {
		this.groupId = groupId;
	}
	
	
	
	
	
	
	

	
}























